package com.longrise.android.byjk.widget.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.longrise.android.byjk.R;
import com.longrise.android.byjk.plugins.course.pay.OnPayItemListener;

/**
 * Created by callxkj on 16/7/25.
 * 余额 微信  支付宝 各个支付的item
 */
public class PayItemView extends RelativeLayout implements View.OnClickListener {

    private  Context mContext;
    private RelativeLayout mRlrootview;
    private ImageView mIvicon;
    private TextView mTvname;
    private ImageView mIvcheckbox;
    private OnPayItemListener mListener;
    private boolean mCheckedStatus;
    private TextView mTvdetail;


    public PayItemView(Context context) {
        super(context);
        init(context);
    }

    public PayItemView(Context context, AttributeSet attrs) {
        super(context,attrs);
        init(context);
    }

    public PayItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context){
        this.mContext = context;
        initView();
        initEvent();
    }

    private void initView(){
        View view = View.inflate(mContext, R.layout.item_payitemview,null);
        mRlrootview = (RelativeLayout) view.findViewById(R.id.item_payment_rl);
        mIvicon = (ImageView) view.findViewById(R.id.item_payment_iv);
        mTvname = (TextView) view.findViewById(R.id.item_payment_name);
        mIvcheckbox = (ImageView) view.findViewById(R.id.item_payment_checkbox);
        mTvdetail = (TextView) view.findViewById(R.id.item_payment_detail);
        addView(view);

    }

   private void initEvent(){
       mRlrootview.setOnClickListener(this);
   }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.item_payment_rl:
                switchStatus(true);
                break;
        }
    }

    public void setImage(int id){
        mIvicon.setImageResource(id);
    }

    public void setName(String name){
        mTvname.setText(name);
    }

    public void setOnPayItemListener(OnPayItemListener listener){
        this.mListener = listener;
    }

    public boolean isChecked(){
        return mCheckedStatus;
    }

    public void setChecked(boolean checked){
        if(mCheckedStatus==checked){
            return ;
        }
        mCheckedStatus = !checked;
        switchStatus(false);
    }

    public void setRightText(String content){
        mTvdetail.setVisibility(View.VISIBLE);
        mTvdetail.setText("（"+content+"）");
    }

    /**
     * 让item不能选择
     */
    public void setUnChecked(){
        mRlrootview.setClickable(false);
        mIvcheckbox.setVisibility(View.GONE);
    }

    /**
     * 选择框还在 但是不能点击
     * @param clickable 是否能点击
     */
    public void setClickable(boolean clickable){
        mRlrootview.setClickable(clickable);
    }

    private void switchStatus(boolean b){
        if(mCheckedStatus){  //已经是选择的状态 点击后变为不是选择的界面
            mIvcheckbox.setImageResource(R.drawable.by_ic_pay_radio_normal);
            mCheckedStatus = false;
            if(mListener!=null&&b){ //click才改变状态
                mListener.onCheck(this,false);
            }
        }else{
            mIvcheckbox.setImageResource(R.drawable.by_ic_pay_radio_selected);
            mCheckedStatus = true;
            if(mListener!=null&&b){
                mListener.onCheck(this,true);
            }
        }
    }


}
